package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.entity.Attachment;
import com.yqg.education.service.IAttachmentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;

import com.yqg.education.service.IProjectPlanService;
import com.yqg.education.entity.ProjectPlan;

import org.springframework.web.bind.annotation.RestController;

/**
 * ProjectPlanController 前端控制器
 *
 * @author zxf
 * @since 2025-09-06
 */
@RestController
@RequestMapping("//project-plan")
public class ProjectPlanController {

    @Resource
    private IProjectPlanService projectPlanService;

    @Resource
    private IAttachmentService attachmentService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody ProjectPlan projectPlan) {

        boolean b = projectPlanService.saveOrUpdate(projectPlan);
        List<Attachment> attachmentList = new ArrayList<>();
        projectPlan.getAttachment().forEach(att -> {
            if (att.getId() == null) {
                Attachment attachment = new Attachment();
                attachment.setCorrelationId(projectPlan.getId());
                attachment.setCorrelationTable("project_plan");
                attachment.setFileName(att.getFileName());
                attachment.setFilePath(att.getFilePath());
                attachmentList.add(attachment);
            }
        });
        if (!attachmentList.isEmpty()) {
            attachmentService.saveOrUpdateBatch(attachmentList);
        }
        return Result.success(b);
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        ProjectPlan projectPlan = projectPlanService.getById(id);
        projectPlan.setIsDelete(1);
        return Result.success(projectPlanService.saveOrUpdate(projectPlan));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<ProjectPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<ProjectPlan> list = projectPlanService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(projectPlanService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) String search) {
        LambdaQueryWrapper<ProjectPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectPlan::getIsDelete, "0")
                .orderByDesc(ProjectPlan::getId);
        if (search != null) {
            queryWrapper.like(ProjectPlan::getProjectPlanNum, search);
        }
        return Result.success(projectPlanService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

